package service

import (
	"chatproject/models"
	"chatproject/utils"
	"fmt"
	"github.com/asaskevich/govalidator"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"strconv"
)

// GetUserList
// @Summary 展示用户信息
// @Tags 用户模块
// @Sources 200 {string} json{"code","message"}
// @Router /user [get]
func GetUserList(c *gin.Context) {
	data := make([]*models.UserBasic, 10)
	data = models.GetUserList()
	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": data,
		"data":    data,
	})
}

// CreateUser
// @Summary 新增用户
// @Tags 用户模块
// @param name query string false "用户名"
// @param password query string false "密码"
// @param repassword query string false "确认密码"
// @Sources 200 {string} json{"code","message"}
// @Router /user [post]
func CreateUser(c *gin.Context) {
	user := models.UserBasic{}
	user.Name = c.Query("name")
	password := c.Query("password")
	repassword := c.Query("repassword")
	salt := fmt.Sprintf("%06d", rand.Int31())
	user.Salt = salt
	data := models.FindUserByName(user.Name)
	if data.Name != "" {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "user name is exist",
			"data": data,
		})
	} else {

		if password != repassword {
			c.JSON(http.StatusForbidden, gin.H{
				"code":    -1,
				"message": "password different",
				"data":    user,
			})
		} else {
			//user.Password = password
			user.Password = utils.MakePassword(password, salt)
			models.CreateUser(user)
			c.JSON(http.StatusOK, gin.H{
				"code":    0,
				"message": "add user success",
				"data":    user,
			})
		}
	}
}

// DeleteUser
// @Summary 删除用户信息
// @Tags 用户模块
// @param id query string false "id"
// @Sources 200 {string} json{"code","message"}
// @Router /user [delete]
func DeleteUser(c *gin.Context) {
	user := models.UserBasic{}
	id, err := strconv.Atoi(c.Query("id"))
	if err != nil {
		fmt.Println(err)
	}
	user.ID = uint(id)
	if user.Name == "" {
		c.JSON(http.StatusForbidden, gin.H{
			"code":    -1,
			"message": "user name is empty",
			"data":    user,
		})
	} else {
		models.DeleteUser(user)
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "delete user success",
			"data":    user,
		})
	}
}

// UpdateUser
// @Summary 修改用户信息
// @Tags 用户模块
// @param id formData string false "id"
// @param name formData string false "name"
// @param password formData string false "password"
// @param phone formData string false "phone"
// @param email formData string false "email"
// @Sources 200 {string} json{"code","message",""}
// @Router /user [put]
func UpdateUser(c *gin.Context) {
	user := models.UserBasic{}
	id, err := strconv.Atoi(c.PostForm("id"))
	if err != nil {
		fmt.Println(err)
	}
	user.ID = uint(id)
	user.Name = c.PostForm("name")
	user.Password = c.PostForm("password")
	user.Phone = c.PostForm("phone")
	user.Email = c.PostForm("email")
	_, err = govalidator.ValidateStruct(user)
	if err != nil {
		fmt.Println(err)
		c.JSON(http.StatusNotFound, gin.H{
			"code":    -1,
			"message": "update user message failure",
			"data":    user,
		})
	} else {
		models.UpdateUser(user)
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "update user success",
			"data":    user,
		})
	}
}

// Login
// @Summary 用户登录
// @Tags 用户模块
// @param name query string false "用户名"
// @param password query string false "密码"
// @Sources 200 {string} json{"code","message"}
// @Router /login [post]
func Login(c *gin.Context) {
	data := models.UserBasic{}
	name := c.Query("name")
	password := c.Query("password")
	user := models.FindUserByName(name)
	if user.Name == "" {
		c.JSON(http.StatusNotFound, gin.H{
			"code": -1,
			"msg":  "user name is not exist",
			"data": data,
		})
	}
	flag := utils.ValidPassword(password, user.Salt, user.Password)
	if !flag {
		c.JSON(http.StatusForbidden, gin.H{
			"code": -1,
			"msg":  "password failure",
			"data": data,
		})
	}
	pwd := utils.MakePassword(password, user.Salt)
	data = models.Login(name, pwd)
	c.JSON(http.StatusOK, gin.H{
		"code":    0, //0成功 -1失败
		"message": "login success",
		"data":    data,
	})
}
